/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package grid;

/**
 *
 * @author Dragan
 */
public class SingleJob {

    protected String vo;
    protected String inputSandbox;
    protected String outputSandbox;
    protected String executable;
    protected String arguments;
    protected String requirements;
    protected Integer defaultNodeRetryCount;
    protected Integer defaultNodeShallowRetryCount;
    protected String rank;

    public SingleJob(String vo, String executable, String arguments, int defaultNodeRetryCount, int defaultNodeShallowRetryCount, String rank) {
        this.vo = "\"" + vo + "\"";
        this.executable = "\"" + executable + "\"";
        this.inputSandbox = "{\"" + executable + "\"}";
        this.arguments = "\"" + arguments + "\"";
        this.defaultNodeRetryCount = defaultNodeRetryCount;
        this.defaultNodeShallowRetryCount = defaultNodeShallowRetryCount;
        this.rank = rank;
    }

    public SingleJob(String vo, String executable, String arguments) {
        this.vo = vo;
        this.executable = executable;
        this.arguments = arguments;
    }

    public void addInput(String input) {
        inputSandbox = inputSandbox.substring(0, inputSandbox.length() - 1) + ", \"" + input + "\"}";

    }

    public void addOutput(String output) {
        if (outputSandbox == null) {
            outputSandbox = "{\"" + output + "\"}";
        } else {
            outputSandbox = outputSandbox.substring(0, outputSandbox.length() - 1) + ", \"" + output + "\"}";
        }
    }

    public String getRequirements() {
        return requirements;
    }

    public void addRequirement(String req) {
        requirements = requirements == null ? req : requirements + "||" + req;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append("[\n");
        builder.append("VirtualOrganisation = " + vo + ";\n");
        builder.append("InputSandbox = " + inputSandbox + ";\n");
        builder.append("Executable = " + executable + ";\n");
        builder.append("Arguments = " + arguments + ";\n");
        builder.append("OutputSandbox = " + outputSandbox + ";\n");
        String req = getRequirements();
        if (req != null) {
            builder.append("Requirements = " + req + ";\n");
        }
        if (rank != null) {
            builder.append("Rank = " + rank + ";\n");
        }
        if (defaultNodeRetryCount != null) {
            builder.append("DefaultNodeRetryCount = " + defaultNodeRetryCount + ";\n");
        }
        if (defaultNodeShallowRetryCount != null) {
            builder.append("DefaultNodeShallowRetryCount = " + defaultNodeShallowRetryCount + ";\n");
        }

        builder.append("]");
        return builder.toString();
    }

    public static void main(String[] args) {
        SingleJob singleJob = new SingleJob("seegrid", "breeder.sh", "grid.Breeder 1 1 1", 0, 3, "-other.GlueCEStateEstimatedResponseTime");
        singleJob.addInput("gridapp.jar");
        singleJob.addInput("population1_1.gz");
        singleJob.addOutput("breeder1_1.gz");
        singleJob.addOutput("population1_1.out.gz");
        System.out.println(singleJob);
    }
}
